<?php
/**
 * Api 接口封装
 */
namespace App\Http\Controllers\Traits;

use Illuminate\Http\JsonResponse;

trait ApiHelper
{
    /**
     * 返回成功响应。
     *
     * @param mixed $content
     * @return JsonResponse
     */
    public function success(mixed $content = []): JsonResponse
    {
        $result = [
            'code' => 200,
            'message' => '成功',
            'type' => 'success',
            'data' => $content
        ];
        return response()->json($result);
    }

    /**
     * 返回错误响应。
     * Return an error response.
     *
     * @param string $message
     * @param int $statusCode
     * @return JsonResponse
     */
    public function error(string $message, int $statusCode = 300): JsonResponse
    {
        $result = [
            'code' => $statusCode,
            'data' => null,
            'message' => $message,
            'type' => 'error',
        ];
        return response()->json($result);
    }
}
